package com.pubinfo.passbook.common.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.pubinfo.passbook.common.model.bo.witeoff.InvoiceInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 报账发票表
 *
 * @TableName t_witeoff_invoice_info
 */
@TableName(value = "t_witeoff_invoice_info")
@Data
@NoArgsConstructor
@ApiModel(value = "TWiteoffInvoiceInfo", description = "报账发票表,接收前端报账单数据,在报账时,财政票据也传到此对象,规则字段上有说明")
public class TWiteoffInvoiceInfo implements Serializable {
  /**
   *
   */
  @TableId(type = IdType.ASSIGN_UUID, value = "id")
  private String id;
  
  /**
   * 供应商编码
   */
  @ApiModelProperty("供应商编码 关联供应商数据")
  private String supplierCode;
  
  /**
   * 供应商名称
   */
  @ApiModelProperty("供应商名称 关联供应商数据")
  private String supplierName;
  
  /**
   * 发票代码
   */
  @ApiModelProperty("发票代码 财政票据对应 电子票据代码")
  private String invoiceCode;
  
  /**
   * 发票类型
   */
  @ApiModelProperty("发票类型 财政票据对应 票据种类")
  private String invoiceType;
  
  /**
   * 发票金额
   */
  @ApiModelProperty("发票金额 财政票据对应 金额")
  private Double sum;
  
  /**
   * 发票号码
   */
  @ApiModelProperty("发票号码 财政票据对应 电子票据号码")
  private String invoiceNo;
  
  /**
   * 开票日期
   */
  @ApiModelProperty("开票日期 YYYY-MM-DD")
  private String invoiceDate;
  
  /**
   * 发票价款
   */
  @ApiModelProperty("发票价款 财政票据对应 金额")
  private Double invoicePrice;
  
  /**
   * 发票税款
   */
  @ApiModelProperty("发票税款 财政票据对应 0")
  private Double taxAmount;
  
  /**
   * 合计
   */
  @ApiModelProperty("合计  发票价款加上发票税款 财政票据对应金额")
  private Double total;
  
  /**
   * 报账单id
   */
  @ApiModelProperty(hidden = true)
  private String witeoffInstanceId;
  
  @ApiModelProperty("可报销金额")
  private String claimAmount;
  
  @TableField(exist = false)
  private static final long serialVersionUID = 1L;
  
  public TWiteoffInvoiceInfo(BigDecimal approvedAmount) {
    this.sum = approvedAmount.doubleValue();
  }
  
  public static List<TWiteoffInvoiceInfo> toList(BigDecimal approvedAmount) {
    List<TWiteoffInvoiceInfo> invoiceInfos = new ArrayList<>();
    
    TWiteoffInvoiceInfo info = new TWiteoffInvoiceInfo(approvedAmount);
    invoiceInfos.add(info);
    return invoiceInfos;
  }
  
  /**
   * @param supplier 供应商
   * @param info     票据或者是发票信息
   * @param type     类型 0 为发票，1为票据
   */
  @SneakyThrows
  public TWiteoffInvoiceInfo(TMdmSupplier supplier, InvoiceInfo info, Integer type, String claimAmount) {
    if (supplier != null) {
      this.supplierCode = supplier.getLifnr();
      this.supplierName = supplier.getName1();
    }
    this.claimAmount = claimAmount;
    this.invoiceNo = info.getFphm();
    this.invoiceCode = info.getFpdm();
    if (0 == type) {
      this.invoiceDate = info.getKprq();
      this.total = Double.valueOf(info.getJshj());
      this.sum = Double.valueOf(info.getJe());
      BigDecimal jshj = new BigDecimal(info.getJshj());
      BigDecimal se = new BigDecimal(info.getSe());
      this.invoicePrice = jshj.subtract(se).doubleValue();
      this.taxAmount = Double.valueOf(info.getSe()); //先设置税款为税额
    } else {
      String kprq = info.getKprq();
      if ("0".equals(kprq)) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(kprq);
        this.invoiceDate = format2.format(date);
      }
      this.total = Double.valueOf(info.getSe());
      this.sum = Double.valueOf(info.getSe());
      this.invoicePrice = Double.valueOf(info.getSe());
      this.taxAmount = Double.valueOf(0); //先设置税款为税额
    }
    
  }
  
}